﻿using Decoration.IService;
using Decoration.Models;
using Decoration.Models.DTO;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserRoleController : ControllerBase
    {
        IUserRoleService svc;
        public UserRoleController(IUserRoleService svc)
        {
            this.svc = svc;
        }

        // GET: api/<UserRoleController>
        [HttpGet]
        public PagedResult<Base_UserRole> Get(int pageIndex = 1, int pageSize = 10)
        {
            var data = svc.Query(o => true);
            int total = data.Count();
            data = data.OrderByDescending(o => o.CreateTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
            return new PagedResult<Base_UserRole>() { code = 1, msg = "成功", data = data, total = total };
        }

        // GET api/<UserRoleController>/5
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            return new Result() { code = 1, msg = "成功", data = info };
        }

        // POST api/<UserRoleController>
        [HttpPost]
        public Result Post(Base_UserRole ur)
        {
            var info = new Base_UserRole()
            {
                CreateTime = DateTime.Now,
                RoleId = ur.RoleId,
                Status = ur.Status,
                UserId = ur.UserId,
            };
            if (info == null)
            {
                return new Result() { code = -1, msg = "数据不能为空" };
            }
            var res=svc.Add(info);
            return new Result() { code = 1, msg="成功",data=res};
        }

        // PUT api/<UserRoleController>/5
        [HttpPut]
        public Result Put(Base_UserRole ur)
        {
            var info = svc.Get(o => o.Id == ur.Id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            info.Status = ur.Status;
            info.UserId = ur.UserId;
            info.RoleId= ur.RoleId;
            var res=svc.Update(info);
            return new Result() { code = 1,msg="成功",data=res};

        }

        // DELETE api/<UserRoleController>/5
        [HttpDelete("{id}")]
        public Result Delete(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            info.Status = 0;
            var res = svc.Update(info);
            return new Result() { code = 1, msg = "成功", data = res };
        }
        [HttpGet]
        [Route("UserRoles")]
        public Result GetUserRoles(int UserId)
        {
            var query = svc.Query(o => o.UserId == UserId && o.Status == 1).ToList();
            return new Result() { code = 1, msg = "成功", data = query };
        }
        [Route("AddUserRole")]
        [HttpPost]
        public Result AddUserRole(UserRoleDto dto)
        {
            //先查找，再删除
            var list = svc.Query(o => o.UserId == dto.UserId).ToList();
            var res = svc.Delete(list, false);//先不提交

            var rms = new List<Base_UserRole>();
            foreach (var item in dto.RoleIds)
            {
                rms.Add(new Base_UserRole() { UserId = dto.UserId, RoleId = item, Status = 1, CreateTime = DateTime.Now });
            }
            res = svc.Add(rms, true);//最后提交
            if (res)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }
        }
    }
}
